package com.netty.demo.protobufexample;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Random;

public class ProtobufClientHandler extends SimpleChannelInboundHandler<DataInfo.MyMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DataInfo.MyMessage msg) throws Exception {

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        int i = new Random().nextInt(3);
        DataInfo.MyMessage message=null;
        if (i == 0) {
            message = DataInfo.MyMessage.newBuilder().setMessageType(DataInfo.MyMessage.MessageType.StudentType)
                    .setStudent(DataInfo.Student.newBuilder().setAddress("中山")
                            .setName("张三")
                            .setId(1).build()
                    ).build();
        }else if(i==1){
            message=  DataInfo.MyMessage.newBuilder().setMessageType(DataInfo.MyMessage.MessageType.DogType)
                    .setDog(DataInfo.Dog.newBuilder().setCity("中山")
                            .setName("张狗").build()
                            ).build();

        }else{
            message=  DataInfo.MyMessage.newBuilder()
                    .setMessageType(DataInfo.MyMessage.MessageType.CatType)
                    .setCat(DataInfo.Cat.newBuilder().setAge(3)
                            .setName("张猫").build()
                    ).build();

        }

        ctx.channel().writeAndFlush(message);
    }
}
